<?php

// $Id$

global $WSC;


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_item_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once('i_menu_item_instance.class.php');


class TCMS_Menu_Item_VPage_DBC extends TCMS_Menu_Item_DBC implements Menu_Item_Instance {
	private $vpage_id = 0;
	private $can_publish = false;

	public function load_by_id(DB_Connection $dbc, $id) {
		$ret = parent::load_by_id($dbc, $id);
		if ($ret) {
			$sql = "select vpage from tcms_menu_items_vpage where menu_item = $id";
			$rez = $dbc->execute($sql);
			if (!$rez->is_eod()) {
				$rez_obj = $rez->fetch_object();
				$this->set_vpage_id($rez_obj->VPAGE);

				if ($this->get_vpage_id()) {
					$vpage = new TCMS_VPage_DBC();
					if ($vpage->load_by_id($dbc, $this->get_vpage_id())) {
						if ($vpage->get_online()) {
							$this->can_publish = true;
						}
					}
				}
			}
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$id = parent::insert($dbc);
		$sql = "insert into tcms_menu_items_vpage (menu_item, vpage) values ($id, {$this->get_vpage()}) ";
		$dbc->execute($sql);

		return $id;
	}


	protected function update(DB_Connection $dbc) {
		$id = parent::update($dbc);
		$this->delete_existing($dbc, $this->get_id());
		$sql = "insert into tcms_menu_items_vpage (menu_item, vpage) values ($id, {$this->get_vpage_id()}) ";
		$dbc->execute($sql);

		return $id;
	}


	public function get_url(DB_Connection $dbc) {
		$ret = '';

		$vpage_id = $this->get_vpage_id();
		if ($vpage_id) {
			$vpage = new TCMS_VPage_DBC();
			if ($vpage->load_by_id($dbc, $vpage_id)) {
				$ret = $vpage->get_path();
			}
		}

		return $ret;
	}


	public function set_vpage_id($vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);

		$this->vpage_id = $vpage_id;
	}


	public function get_vpage_id() {
		return $this->vpage_id;
	}


	public function can_publish() {
		return $this->can_publish;
	}


	public static function exists(DB_Connection $dbc, $item_id, $vpage_id) {
		tangra_if_not_int_throw_e($item_id);
		tangra_if_not_int_throw_e($vpage_id);

		$sql = "select menu_item from tcms_menu_items_vpage where menu_item = $item_id and vpage = $vpage_id";
		$rez = $dbc->execute($sql);

		return !$rez->is_eod();
	}


	private function delete_existing($dbc, $item_id) {
		$sql = "delete from tcms_menu_items_vpage where menu_item = $item_id";

		$rez = $dbc->execute($sql);
	}


	public static function delete(DB_Connection $dbc, $item_id) {
		tangra_if_not_int_throw_e($item_id);

		$sql = "delete from tcms_menu_items_vpage where menu_item = $item_id";
		$rez = $dbc->execute($sql);

		TCMS_Menu_Item_DBC::delete($dbc, $item_id);
	}


	public static function load_items_linking_to_vpage(DB_Connection $dbc, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);

		$ret = array();

		$sql = "select menu_item from tcms_menu_items_vpage where vpage = $vpage_id";
		$rez = $dbc->execute($sql);
		while($rez_obj = $rez->fetch_object()) {
			$path = array();
			self::prepare_item_full_path($dbc, $rez_obj->MENU_ITEM, $path);
			$path = array_reverse($path);
			$ret[] = $path;
		}

		return $ret;
	}


	private static function prepare_item_full_path(DB_Connection $dbc, $menu_item_vpage_id, &$path) {
		tangra_if_not_int_throw_e($menu_item_vpage_id);
		$tmp_arr = array();

		$item = new TCMS_Menu_Item_DBC();
		if ($item->load_by_id($dbc, $menu_item_vpage_id)) {
			$path[] = array('id' => $item->get_id(), 'label' => $item->get_label(), 'menu_id' => $item->get_menu(), 'published' => $item->get_published());
			if ($item->get_parent()) {
				self::prepare_item_full_path($dbc, $item->get_parent(), $path);
			} else {
				$menu = new TCMS_Menu_DBC();
				if ($menu->load_by_id($dbc, $item->get_menu())) {
					$path[] = array('id' => $menu->get_id(), 'title' => $menu->get_name());
				}
			}
		}
	}


	public static function unpublish_items_for_vpage(DB_Connection $dbc, $vpage_id) {
		$sql = "update tcms_menu_items mi, tcms_menu_items_vpage miv set mi.published = 0 where miv.vpage = $vpage_id and miv.menu_item = mi.id";
		$rez = $dbc->execute($sql);
	}


	public static function delete_for_menu_item(DB_Connection $dbc, $menu_item_id) {
		tangra_if_not_int_throw_e($menu_item_id);

		$sql = "delete from tcms_menu_items_vpage where menu_item = $menu_item_id";
		$rez = $dbc->execute($sql);
	}


	public static function load_ids_for_items_linking_to_vpage(DB_Connection $dbc, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);
		$ret = array();

		$sql = "select menu_item from tcms_menu_items_vpage where vpage = $vpage_id";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret[] = $rez_obj->MENU_ITEM;
		}

		return $ret;
	}
}